package com.wizz.trade.trading.controller;

import com.wizz.trade.common.PageVo;
import com.wizz.trade.common.ResponseResult;
import com.wizz.trade.common.constant.RequestQueryTypeConstant;
import com.wizz.trade.common.constant.StatusCode;
import com.wizz.trade.trading.dto.ConsDto;
import com.wizz.trade.trading.dto.ConsRequestDto;
import com.wizz.trade.trading.dto.page.ConsPageDto;
import com.wizz.trade.trading.dto.page.RequestPageDto;
import com.wizz.trade.trading.dto.page.TradePageDto;
import com.wizz.trade.trading.service.ConsService;
import com.wizz.trade.trading.service.RequestService;
import com.wizz.trade.trading.service.TradeService;
import com.wizz.trade.trading.vo.ConsVo;
import com.wizz.trade.trading.vo.StampDutyRequestVo;
import com.wizz.trade.trading.vo.TradeForSecondPageVo;
import com.wizz.trade.trading.vo.basic.RequestVo;
import com.wizz.trade.util.FileOperationUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 贸易模块/合同管理&印花税付款申请
 *
 * @author fyftless
 * @since 2023-07-25 08:15:51
 */
@RestController
@RequestMapping("/contract")
public class TradeSecondController {

    @Resource
    private ConsService consService;

    @Resource
    private RequestService requestService;

    @Resource
    private TradeService tradeService;

    /**
     * 生成合同
     * @param consDto 合同信息
     * @return response
     */
    @PostMapping("/contract")
    public ResponseResult<String> generateContract(@RequestBody ConsDto consDto){
        consService.addCons(consDto);
        return new ResponseResult<>(StatusCode.OK, "添加成功");
    }

    /**
     * 印花税付款申请
     * @param consRequestDto 申请信息
     * @return response
     */
    @PostMapping("/request")
    public ResponseResult<String> generateRequest(@RequestBody ConsRequestDto consRequestDto){
        requestService.addRequestByConsIds(consRequestDto);
        return new ResponseResult<>(StatusCode.OK, "添加成功");
    }

    /**
     * 分页查询合同
     * @param consPageDto 分页条件
     * @return response
     */
    @GetMapping("/contract")
    public ResponseResult<PageVo<ConsVo>> getConsPage(ConsPageDto consPageDto){
        PageVo<ConsVo> page = consService.getPage(consPageDto);
        return new ResponseResult<>(StatusCode.OK, "查询成功", page);
    }

    /**
     * 分页查询付款申请(queryType=1)
     * @param requestPageDto 分页条件
     * @return response
     */
    @GetMapping("/request")
    public ResponseResult<PageVo<? extends RequestVo>> getRequestPage(RequestPageDto requestPageDto){
        PageVo<? extends RequestVo> page = null;
        if (requestPageDto.getQueryType() == RequestQueryTypeConstant.STAMP_DUTY){
            page = requestService.getPageForStampDuty(requestPageDto);
        } else if (requestPageDto.getQueryType() == RequestQueryTypeConstant.TRADE) {
            page = requestService.getPageForTrade(requestPageDto);
        }
        return new ResponseResult<>(StatusCode.OK, "查询成功", page);
    }

    /**
     * 合同归档
     * @return response
     */
    @PutMapping("/contract/filing")
    public ResponseResult<String> contractFiling(@RequestBody List<Integer> consIds){
        consService.contractFiling(consIds);
        return new ResponseResult<>(StatusCode.OK, "更新成功");
    }

    /**
     * 合同扫描件上传
     * @param multipartFile 上传的文件
     * @param id 合同id
     * @return 上传结果
     */
    @PostMapping("/contract/image/{id}")
    public ResponseResult<String> uploadConsImage(@RequestParam("file") MultipartFile multipartFile, @PathVariable("id") Integer id){
        String url = consService.uploadConsImage(id, multipartFile);
        return new ResponseResult<>(StatusCode.OK, "上传成功", url);
    }

    /**
     * 印花税付款申请单扫描件上传
     * @param multipartFile 上传的文件
     * @param id 申请单id
     * @return 上传结果
     */
    @PostMapping("/request/image/{id}")
    public ResponseResult<String> uploadRequestImage(@RequestParam("file") MultipartFile multipartFile, @PathVariable("id") Integer id){
        String url = requestService.uploadRequestImage(id, multipartFile);
        return new ResponseResult<>(StatusCode.OK, "上传成功", url);
    }

    /**
     * 查询第二页需要显示的贸易信息
     * @param tradePageDto 分页条件
     * @return response
     */
    @GetMapping("/trade")
    public ResponseResult<PageVo<TradeForSecondPageVo>> getTrades(TradePageDto tradePageDto){
        PageVo<TradeForSecondPageVo> pageForSecondPage = tradeService.getPageForSecondPage(tradePageDto);
        return new ResponseResult<>(StatusCode.OK, "查询成功", pageForSecondPage);
    }
}
